package com.brkj.course;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import com.brkj.course.model.DS_CourseClassInfo;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.main.R;
import com.brkj.model.DS_Course;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.view.BaseCoreActivity;
import com.brkj.util.view.PullListView;
import com.dgl.sdk.util.AdaptListHeight;
import com.dgl.sdk.view.NoSlipListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dgl on 2014/4/25.
 */
public class CourseViewClass {

    private PullListView courseListView;
    private TextView noContent;

    private View view;
    private Context mContext;

    private CourseAdapter courseAdapter;
    List<DS_Course> courseList = new ArrayList<DS_Course>();
    private boolean hasShown = false;

    private ListView courseClassInfoListView;
    private CourseClassInfoAdapter courseClassInfoAdapter;
    private List<DS_CourseClassInfo> courseClassInfo;
    private PopupWindow popupWindow;

    private Button infoButton;
    private TextView infoCourseNum;


    public CourseViewClass(Context context){
        mContext = context;
    }

    public View getView(){
        view =  LayoutInflater.from(mContext).inflate(R.layout.course_class_view, null);

        courseClassInfoListView = (ListView) view.findViewById(R.id.courseClassInfoListView);

        courseListView = (PullListView)view.findViewById(R.id.courseListView);
        infoButton = (Button)view.findViewById(R.id.infoButton);
        infoCourseNum = (TextView)view.findViewById(R.id.infoCourseNum);

        noContent = (TextView)view.findViewById(R.id.noContent);

        infoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showInfo(true);
            }
        });


//        initCourseClassPopUpWindow();
        getCourseClassInfo();

        return view;
    }

    public void show(){
        if((hasShown)&&(courseClassInfo!= null)&& (courseClassInfo.size()>0)){
            return;
        }else{
            showInfo(true);
            hasShown = true;
        }

    }

    private void showInfo(Boolean show){
        if(show){
            courseClassInfoListView.setVisibility(View.VISIBLE);
        }else{
            courseClassInfoListView.setVisibility(View.GONE);
        }
    }

    public void infoClassClick(DS_CourseClassInfo info){
        infoButton.setText(info.getName());
        infoCourseNum.setText("共"+ info.getCourseNum() + "课");

        showInfo(false);

        getCourse(info.getClassId(), 0);
    }

    private void initCourseClassPopUpWindow(){

        View popupWindow_view = LayoutInflater.from(mContext).inflate(
                R.layout.course_class_info_popupwindow, null, false);
        popupWindow = new PopupWindow(popupWindow_view,
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT, false);// 鍒涘缓PopupWindow瀹炰緥

        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        // popupWindow.setAnimationStyle(R.style.PopWindowAnim);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0));

        courseClassInfoListView = (ListView) popupWindow_view.findViewById(R.id.courseClassInfoListView);

    }

    private void  getCourseClassInfo(){
        BaseAjaxParams params = new BaseAjaxParams();

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetCourseClassInfo.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)mContext) {

            @Override
            public void onSuccess(Object t) {
                super.onSuccess(t);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_CourseClassInfo>>(){}.getType();
                courseClassInfo = gson.fromJson((String)t, listType);

                if((courseClassInfo!= null) && (courseClassInfo.size()>0)){
                    courseClassInfoAdapter = new CourseClassInfoAdapter(mContext, courseClassInfo, CourseViewClass.this);
                    courseClassInfoListView.setAdapter(courseClassInfoAdapter);
                }
            }
        });
    }


    private void getCourse(final int CourseClassID, final int CourseID) {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetClassCourse.params.CourseClassID, Integer.toString(CourseClassID));
        params.put(HttpInterface.HIF_GetClassCourse.params.CourseID, Integer.toString(CourseID));

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetClassCourse.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)mContext) {


            @Override
            public void onFailure(Throwable t, String strMsg) {
                // TODO Auto-generated method stub
                super.onFailure(t, strMsg);
                courseListView.onRefreshComplete();
            }

            @Override
            public void onSuccess(Object t) {
                super.onSuccess(t);

//				HIF_GetCourse.result result = JsonUtil.fromJson((String)t, HIF_GetCourse.result.class);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_Course>>(){}.getType();
                List<DS_Course> courseListTmp = gson.fromJson((String)t, listType);

                if((courseListTmp != null) && (courseListTmp.size() > 0)){
                    if(courseList.size()>0){
                        //加载更多
                        courseList.addAll(courseListTmp);
                        courseAdapter.notifyDataSetInvalidated();
                    }else{
                        courseList.addAll(courseListTmp);
                        fillView(CourseClassID);
                    }


                }else{
                    courseListView.setVisibility(View.GONE);
                    noContent.setVisibility(View.VISIBLE);
                    noContent.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            getCourse(CourseClassID, 0);
                        }
                    });
                    ((BaseCoreActivity)mContext).showToast("未获取到课程！");
                }

                courseListView.onRefreshComplete();
            }

        });

    }


    private void fillView(final int courseClassID){

        courseListView.setVisibility(View.VISIBLE);
        courseAdapter = new CourseAdapter(mContext, courseList);
        courseListView.setAdapter(courseAdapter);

        courseListView.setonRefreshListener(new PullListView.OnRefreshListener() {

            @Override
            public void onRefresh() {
                getCourse(courseClassID, 0);

            }
        });

        if(courseList.size()>=9){
            courseListView.unHideFooterView();
            courseListView.setonGetMoreListener(new PullListView.OnGetMoreListener() {

                @Override
                public void onGetMore() {
                    int courseId = courseList.get(courseList.size()-1).getCourseID();
                    getCourse(courseClassID, courseId);

                }
            });
        }else{
            courseListView.hideFooterView();
        }
    }
}
